const Koa = require("koa");
const app = new Koa();
const onerror = require("koa-onerror");

// error handler
onerror(app);

const register_routes = require("./routes");
const Model = require("./models");
const Service = require("./services");
const Controller = require("./controller");
const Middleware = require("./middleware");
const boot = require("./bootstrap");
const tasks = require("./tasks");
Model.register()
  .then(() => Service.register())
  .then(() => Controller.register())
  .then(() => Middleware.register(app))
  .then(() => register_routes(app))
  .then(() => boot(app))
  .then(() => tasks.run(app))
  .then(() => {
    //注册
    // Service.service.user.join({
    //   name: "章三",
    //   phone: "13566625826",
    //   password: "123456",
    //   type: 1,
    // });
  });

// error-handling
app.on("error", (err, ctx) => {
  console.error("server error", err, ctx);
});

module.exports = app;
